package com.qf07;

public class BreadCon {

    //存放面包的数组
    private Bread[] cons=new Bread[6];
    //存放面包的位置
    private int index;

    //存面包
    public synchronized void input(Bread b){
        //判断容器有没有满
        if (index>=6){
            try {
                this.wait();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }

        cons[index]=b;
        System.out.println(Thread.currentThread().getName()+"生产了"+b.getId()+"");
        index++;
        //唤醒
        this.notifyAll();
    }
    //取出面包
    public synchronized void output(){//锁this
        if (index<=0){
            try {
                this.wait();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        index--;
        Bread b=cons[index];
        System.out.println(Thread.currentThread().getName()+"消费了"+b.getId()+" 生产者:"+b.getProductNamme());
        cons[index]=null;
        //唤醒生产者
        this.notifyAll();
    }
}
